/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.Ingredient;

/**
 *
 * @author Morten
 */
public class IngredientHandler {

    private DBLogin db;
    private ResultSet rs;
    private ArrayList<Ingredient> ingredients;

    public IngredientHandler(DBLogin db) {
	this.db = db;
	ingredients = new ArrayList<>();
    }

    public void createIngredientList() {
	try {
	    String sql = "SELECT * FROM ingredient ORDER BY in_name";
	    rs = db.getStmt().executeQuery(sql);
	    while (rs.next()) {
		Ingredient ing;
		ing = new Ingredient(Integer.parseInt(rs.getString("id")), rs.getString("in_name"), rs.getInt("in_type"), rs.getDate("expire_date"), rs.getString("unit"), rs.getInt("stock"));
		if (ing != null) {
		    ingredients.add(ing);
		}

	    }
	} catch (SQLException ex) {
	    System.out.println("SQL Fejl i IH " + ex);
	}
    }

    public Ingredient getIngredient(int x) {
	Ingredient ingRet=null;
	for (Ingredient ing : ingredients) {
	    if(ing.getId()==x){
		ingRet=ing;
	    }
	}
	return ingRet;
    }

    public ArrayList<Ingredient> getIngredientList() {
	return ingredients;
    }

}
